$(document).ready(function() {

	//ajaxform
	var options = {
        dataType: 'json',
        beforeSubmit: showRequest, //表单验证方式
        success: showResponse // post-submit callback 
    };
    $('#ajaxForm').ajaxForm(options);

    function showResponse(response, status) {
        $('#ajaxForm button').removeAttr('disabled');
        $('#ajaxForm input').removeAttr('disabled');
        if (status == 'success') {
            layer.close(load);
            if (response.status == 1) {
                //清空购物车
                if (response.info=='订单提交并支付成功') {
                    localStorage.removeItem("cart");
                }
                layer.open({
                    type: 0,
                    content: response.info,
                    time: 3,
                    end: function() {
                        if (response.url != '' && response.url != undefined && response.url != "undefined") {
                            if (response.url == 'reload') {
                                window.location.reload();
                            } else {
                                window.location.href = response.url;
                            }
                        }
                    }
                });
            } else {
                layer.open({
                    type: 0,
                    time: 3,
                    content: response.info
                });
            }
        } else {
            layer.msg('服务器错误');
        }
    } 

    function showRequest(){
        FormStatus = $("form.form-validata").valid();
        if (FormStatus){
            $('#ajaxForm button').attr("disabled", "disabled");
            $('#ajaxForm input').attr("disabled", "disabled");
            load = layer.open({
                type: 2
            });
        }
        return FormStatus;
    }

	//validation验证
	$("form.form-validata").validate({
		errorElement: "p",
        errorClass: "error",
	})

	$.validator.addMethod("phone",function(value,element){
        return this.optional(element) || testPhone(value);
    },"Phone format error");

});

function testPhone(v){
    if(v==""){
        return false;
    }else{
        var pattern = /^((?:\d{9})|(?:\d{3}-\d{4})|(?:\d{3}-\d{3}-\d{4})|[0-9]{10})$/;        
        flag = pattern.test(v);
        if(!flag){
            return false;
        }else{
            return true;
        }
    }
}